﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using ShopLib.DA;

namespace ShopLib.shop.Basket
{
    /// <summary>
    /// Summary description for BasketGridView
    /// </summary>
    public class BasketGridView : GridView
    {
        public BasketGridView()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        // Fields
        private Guid _orderId;
        private string _sortBy = null;
        private double priceTotal = 0;
        private double weightTotal = 0;
        private double quantityTotal = 0;
        private double grandTotal = 0;
        //Methods
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            DABasis da = new DABasis();

            // Add additional this.* to set other properties for 'this' control. 
            // In this case our derived GridView

            this.DataSource = da.GetBasketContent(OrderId, SortBy);
            this.RowDataBound += new GridViewRowEventHandler(BasketGridView_RowDataBound);

            this.DataBind();
        }

        void BasketGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Price"));

                // add the UnitPrice and QuantityTotal to the running total variables
                priceTotal += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Price"));
                quantityTotal += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Quantity"));
                grandTotal += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Price")) * Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Quantity"));
                e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
                e.Row.Cells[3].Text = ((Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Quantity"))) * (Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Price")))).ToString("c");
                e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[1].Text = "Ialt inkl. moms:";
                // for the Footer, display the running totals
                e.Row.Cells[2].Text = quantityTotal.ToString();
                e.Row.Cells[3].Text = grandTotal.ToString("c");

                e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
                e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
                e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
                e.Row.Font.Bold = true;
            }
        }

        //Properties
        public Guid OrderId
        {
            get { return _orderId; }
            set { _orderId = value; }
        }
        public string SortBy
        {
            get { return _sortBy; }
            set { _sortBy = value; }
        }

    }
}